package cn.ds.fly.modules.sys.service.impl;

import cn.ds.fly.modules.sys.entity.FlyMenu;
import cn.ds.fly.modules.sys.mapper.MenuMapper;
import cn.ds.fly.modules.sys.service.MenuService;
import cn.ds.fly.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lds on 2017/12/14.
 * @author lds
 */
@Service("menuService")
public class MenuServiceImpl extends BaseServiceImpl<MenuMapper,FlyMenu> implements MenuService {

    @Override
    public List<FlyMenu> getUserMenuList(String userId) {
        if(StringUtils.isBlank(userId)){
            return null;
        }
        List<FlyMenu> resultMenuList;
        //如果是管理员，拥有最高权限
        if(Constant.SUPER_ADMIN.equals(userId)){
            List<FlyMenu> menuList = mapper.queryAllMenu();
            resultMenuList = new ArrayList<>(menuList.size());
            //return getAllMenuList(null);
            buildMenuTree(menuList,resultMenuList,"0");
            return resultMenuList;
        }
        //非管理员用户
        return getAllMenuList(queryAllMenuId(userId));
    }

    private List<FlyMenu> buildMenuTree(List<FlyMenu> menuList, List<FlyMenu> resultList, String parentId){
        if(menuList==null || menuList.size()<1){
            return menuList;
        }
        List<FlyMenu> subMenuList = new ArrayList<>();
        for (FlyMenu menu:menuList){
            if(menu==null){
                continue;
            }
            if(parentId.equals(menu.getParentId())){
                if(Constant.MenuType.CATALOG.getValue()==menu.getType()){
                    menu.setList(buildMenuTree(menuList,resultList,menu.getId()));
                    resultList.add(menu);
                }else{
                    subMenuList.add(menu);
                }
            }
        }

        return subMenuList;
    }

    private List<FlyMenu> getAllMenuList(List<String> menuIdList) {
        //查询根菜单
        List<FlyMenu> menuList = queryListParentId("0",menuIdList);
        if(menuList==null || menuList.size()<1){
            return menuList;
        }
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    private List<FlyMenu> getMenuTreeList(List<FlyMenu> menuList, List<String> menuIdList) {
        if(menuList==null||menuList.size()<1){
            return null;
        }
        List<FlyMenu> subMenuList = new ArrayList<>();
        for (FlyMenu menu : menuList){
            if(menu==null){
                continue;
            }
            //如果为目录
            if(menu.getType() == Constant.MenuType.CATALOG.getValue()){
                menu.setList(getMenuTreeList(queryListParentId(menu.getId(),menuIdList),menuIdList));
            }
            subMenuList.add(menu);
        }
        return subMenuList;
    }

    private List<FlyMenu> queryListParentId(String parentId, List<String> menuIdList) {
        if(StringUtils.isBlank(parentId)){
            return null;
        }
        //查询父类下的一级子菜单
        List<FlyMenu> menuList = queryListParentId(parentId);
        //判断是否需要继续过滤数据
        if(menuIdList==null||menuIdList.size()<1||menuList==null||menuList.size()<1){
            return menuList;
        }
        //过滤当前用户下的菜单
        List<FlyMenu> userMenuList = new ArrayList<>();
        for (FlyMenu menu : menuList){
            if(menuIdList.contains(menu.getId())){
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    private List<FlyMenu> queryListParentId(String parentId) {
        if(StringUtils.isBlank(parentId)){
            return null;
        }
        return mapper.queryListParentId(parentId);
    }

    @Override
    public List<String> queryAllMenuId(String userId) {
        if(StringUtils.isBlank(userId)){
            return null;
        }
        return mapper.queryAllMenuId(userId);
    }


}
